package com.itcs.se.security.tran;

import java.util.HashMap;
import java.util.Map;

import com.itcs.eureka.service.PayApiFeignClientService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.itcs.box.BoxConstants;
import com.itcs.box.domain.BoxData;
import com.itcs.box.domain.BoxRequest;
import com.itcs.box.domain.BoxResponse;
import com.itcs.box.util.DateUtils;
import com.itcs.box.util.LogUtils;
import com.itcs.se.security.data.req.Box1022Req;
import com.itcs.se.security.data.res.Box2022Res;
import com.itcs.se.security.service.SeSecurityService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 功能描述:
 * <贵州SE发行请求>
 *
 * @ClassName: BoxTranModeWithA1022
 * @Author: wanghui
 * @Date: 2019年05月16日 10:43:35
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
@Component
public class BoxTranModeWithA1022 implements PayApiFeignClientService {

	private Log logger = LogFactory.getLog(getClass());

	@Autowired
	private SeSecurityService seSecurityService;

	@Override
	public String getTrancode() {
		return "box_A1022";
	}

	@Override
	@RequestMapping(value = "/box_A1022", method = RequestMethod.POST, produces = "application/json")
	public Map<String, Object> exchange(String serverId, String trancode,
										@RequestParam Map<String, Object> reqRecord) {

		logger.info(LogUtils.build("A1022", "SE发行请求模式A", "上传数据:[" + JSON.toJSONString(reqRecord) + "]"));
		
		BoxResponse boxResponse = new BoxResponse();
		BoxData responseData = new BoxData();
		boxResponse.setData(responseData);

		Map<String, Object> res = new HashMap<>();
		try {
			// 业务处理
			BoxRequest boxRequest = (BoxRequest) reqRecord.get(BoxConstants.BOX_REQ_KEY);
			boxResponse = this.handle(boxRequest, boxResponse);
		} catch (Exception e) {
			// 其他异常或数据库异常
			responseData.setResult("9");
		}
		res.put(BoxConstants.BOX_RES_KEY, boxResponse);

		return res;
	}

	public BoxResponse handle(BoxRequest boxRequest, BoxResponse boxResponse) {

		boxResponse.setMessgeType("2022");

		BoxData data = boxRequest.getData();
		String deviceId = data.getDeviceId();
		BoxData responseData = new BoxData();

		Box1022Req req = new Box1022Req(data.getSeSn(), data.getSePK(), data.getSeR());
		req.setClientId(deviceId);
		Box2022Res res = seSecurityService.doIssueRequestForGZ(req);

		// 设备ID
		responseData.setDeviceId(data.getDeviceId());

		if (res.getErrCode().equals("0")) {
			// 成功
			responseData.setResult("1");
			// 写0015文件指令
			responseData.setSeWrite0015(res.getSeWrite0015());
			// 写渠道公钥指令
			responseData.setSeWritePK(res.getSeWritePK());
		} else {
			// 验证签名失败
			responseData.setResult("9");
		}
		// 回复时间
		responseData.setReplyTime(DateUtils.getUnixTimeStamp());
		boxResponse.setData(responseData);

		return boxResponse;
	}

}
